/**
 * 权限校验
 * @param { String } scopeStr 需要鉴权的功能码 
 * @returns
 */
export function checkPermisson(scopeStr) {
	return new Promise((resolve, reject) => {
		wx.getSetting({
			success: async (res) => {
				const authSettingObj = res.authSetting
				if (authSettingObj[scopeStr] === true) {
					// 当前已经有权限了
					console.log('当前已经有权限了', scopeStr)
					return resolve(true)
				} else if (authSettingObj[scopeStr] === false) {
					// 之前问过权限，但是拒绝了
					console.log('之前问过权限，但是拒绝了', scopeStr)
					// 打开setting
					try {
						await wx.openSetting(scopeStr)
						return resolve(true)
					} catch {
						return reject(false)
					}
				} else {
					// 还没问过权限
					console.log('还没问过权限', scopeStr)
					// 去请求授权
					wx.authorize({
						scope: scopeStr,
						success: (res) => {
							return resolve(true)
						},
						fail: async (err) => {
							// 拒绝授权了
							// 打开setting
							try {
								await wx.openSetting(scopeStr)
								return resolve(true)
							} catch {
								return reject(false)
							}
						},
					})
				}
			},
		})
	})
}